package com.orifine.orm.information;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import com.orifine.orm.Information;
import com.orifine.orm.model.ColumnModel;

public class TimestampIncrementInfo implements Information {
	private long time;
	private long incrementTime = 86400000;
	private boolean isFixedIncrement = true;
	private Map<Integer, Timestamp> dateMap = new HashMap<Integer, Timestamp>();
	
	public TimestampIncrementInfo(){
		setRandomDate();
		randomIncrement();
		setIncrementDate(THREE_DAY_NUMBER);
	}
	
	@Override
	public Object value(int index, ColumnModel columnMapper) {
		Timestamp date = dateMap.get(index);
		if(date != null){
			return date;
		} else {
			if(isFixedIncrement){
				this.time += incrementTime;
			} else {
				this.time += (long)(Math.random()*incrementTime);
			}
			date = new Timestamp(this.time);
			dateMap.put(index, date);
			return date;
		}
	}

	@Override
	public void init(ColumnModel columnMapper) {
		randomIncrement();
	}
	
	public TimestampIncrementInfo setBeginData(Date beginDate){
		this.time = beginDate.getTime();
		return this;
	}
	
	public TimestampIncrementInfo setNowDate(){
		return setBeginData(new Date(System.currentTimeMillis()));
	}
	
	public TimestampIncrementInfo setRandomDate(){
		this.time = TWO_MILLENNIUM_NUMBER + (long)(Math.random()*TEN_YEAH_NUMBER);
		return this;
	}
	
	public TimestampIncrementInfo randomIncrement(){
		isFixedIncrement = false;
		return this;
	}
	
	public TimestampIncrementInfo fixedIncrement(){
		isFixedIncrement = true;
		return this;
	}
	
	public TimestampIncrementInfo setIncrementDate(long incrementTime){
		this.incrementTime = incrementTime;
		return this;
	}
	
	public static final long THREE_DAY_NUMBER = 3L*24L*60L*60L*1000L;
	public static final long TEN_YEAH_NUMBER = 365L*24L*60L*60L*1000L;
	public static final long TWO_MILLENNIUM_NUMBER = new GregorianCalendar(2000, 1, 1).getTimeInMillis();
}
